﻿using System;
using System.Linq;
using System.Xml.Linq;
using DotNetNuke.Entities.Portals;

namespace HeroldIT.Dnn.Modules.News
{
    public partial class NewsEntryDetail
    {
        #region static methods

        public static NewsEntryDetail Import(XElement toImport)
        {
            if (null == toImport)
                return null;

            if (toImport.Name != "detail")
                throw new ArgumentException("The element to import is not a valid detail export.", "toImport");

            PortalSettings portalSettings = PortalController.GetCurrentPortalSettings();

            NewsEntryDetail imported = new NewsEntryDetail();

            toImport.Elements()
                .All(e =>
                         {
                             switch (e.Name.ToString())
                             {
                                 case "headline":
                                     imported.Headline = e.Value;
                                     break;

                                 case "subline":
                                     imported.Subline = e.Value;
                                     break;

                                 case "teaserText":
                                     imported.TeaserText = e.Value;
                                     break;

                                 case "teaserImageUrl":
                                     imported.TeaserImageUrl = e.Value.Replace("[P]", portalSettings.HomeDirectory);
                                     break;

                                 case "teaserImageCaption":
                                     imported.TeaserImageCaption = e.Value;
                                     break;

                                 case "detailText":
                                     imported.DetailText = e.Value;
                                     break;

                                 case "detailImageUrl":
                                     imported.DetailImageUrl = e.Value.Replace("[P]", portalSettings.HomeDirectory);
                                     break;

                                 case "detailImageCaption":
                                     imported.DetailImageCaption = e.Value;
                                     break;

                                 case "detailUrl":
                                     imported.DetailUrl = e.Value;
                                     break;

                                 case "source":
                                     imported.Source = e.Value;
                                     break;
                             }
                             return true;
                         });
            toImport.Attributes()
                .All(a =>
                         {
                             switch (a.Name.ToString())
                             {
                                 case "locale":
                                     imported.Locale = a.Value;
                                     break;

                                 case "isHidden":
                                     imported.IsHidden = (bool) a;
                                     break;

                                 case "isDefaultLocale":
                                     imported.IsDefaultLocale = (bool) a;
                                     break;

                                 case "useDetailImageForTeaser":
                                     imported.UseDetailImageForTeaser = (bool) a;
                                     break;
                             }
                             return true;
                         });
            if (!imported.IsEmpty())
            {
                return imported;
            }
            return null;
        }

        #endregion

        #region methods

        public bool IsEmpty()
        {
            return
                String.IsNullOrEmpty(this.Headline)
                && String.IsNullOrEmpty(this.TeaserText)
                && this.NewsEntryDetailId == 0
                && !this.IsHidden;
        }

        public XElement Export()
        {
            PortalSettings portalSettings = PortalController.GetCurrentPortalSettings();
            if (this.IsEmpty())
                return null;

            return new XElement("detail",
                    new XAttribute("locale", this.Locale),
                    new XElement("headline", this.Headline),
                    !String.IsNullOrEmpty(this.Subline) ? new XElement("subline", this.Subline) : null,
                    new XElement("teaserText", this.TeaserText),
                    !String.IsNullOrEmpty(this.TeaserImageUrl) ? new XElement("teaserImageUrl", this.TeaserImageUrl.Replace(portalSettings.HomeDirectory, "[P]")) : null,
                    !String.IsNullOrEmpty(this.TeaserImageCaption) ? new XElement("teaserImageCaption", this.TeaserImageCaption) : null,
                    !String.IsNullOrEmpty(this.DetailText) ? new XElement("detailText", this.DetailText) : null,
                    !String.IsNullOrEmpty(this.DetailImageUrl) ? new XElement("detailImageUrl", this.DetailImageUrl.Replace(portalSettings.HomeDirectory, "[P]")) : null,
                    !String.IsNullOrEmpty(this.DetailImageCaption) ? new XElement("detailImageCaption", this.DetailImageCaption) : null,
                    !String.IsNullOrEmpty(this.DetailUrl) ? new XElement("detailUrl", this.DetailUrl) : null,
                    !String.IsNullOrEmpty(this.Source) ? new XElement("source", this.Source) : null,
                    new XAttribute("isHidden", this.IsHidden),
                    new XAttribute("isDefaultLocale", this.IsDefaultLocale),
                    new XAttribute("useDetailImageForTeaser", this.UseDetailImageForTeaser)
                );
        }

        #endregion
    }
}
